package com.hzhr.iot.meterplan.service;

import com.hzhr.iot.core.annotation.ApiImplicitInternalParams;
import com.hzhr.iot.core.annotation.CygService;
import com.hzhr.iot.core.constant.CommonOptions;
import com.hzhr.iot.core.exception.ServiceException;
import com.hzhr.iot.core.util.HzhrDateUtil;
import com.hzhr.iot.core.util.HzhrTypeUtil;
import com.hzhr.iot.login.sdk.model.userCard.UserCardModel;
import com.hzhr.iot.meterplan.dao.mapper.generator.custom.YongqidizhiEntityCustomMapper;
import com.hzhr.iot.meterplan.entity.YongqidizhiEntity;
import com.hzhr.iot.meterplan.entity.ext.RuhujihuaEntityExt;
import com.hzhr.iot.meterplan.entity.ext.YongqidizhiEntityExt;
import com.hzhr.iot.meterplan.model.meterGasUser.MeterGasUserGetModel;
import com.hzhr.iot.meterplan.model.meterGasUser.MeterGasUserModel;
import com.hzhr.iot.meterplan.model.meterGasUser.MeterGasUserPostModel;
import com.hzhr.iot.meterplan.model.meterPlan.MeterPlanGetModel;
import com.hzhr.iot.meterplan.model.meterPlan.MeterPlanModel;
import com.hzhr.iot.meterplan.service.base.BaseService;
import java.util.List;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.ParseException;
import java.util.ArrayList;

import java.util.Date;
import java.util.Map;

@Service
@CygService(entity = YongqidizhiEntity.class, model = MeterGasUserModel.class)
public class MeterGasUserService extends BaseService {

    @Autowired
    YongqidizhiEntityCustomMapper yongqidizhiEntityCustomMapper;

    //region javadoc注释 : get
    /**
     * 简介：查询登录用户管辖范围内的抄表用户 <br/><br/>
     * 步骤： <br/>
     * 1.查询登录用户管辖范围内的并且状态为“在用”燃气用户 <br/>
     * 注意以下查询规则： <br/>
     * (1) 在query参数notequal_meterPlanId不为空，如果查询出的燃气用户在某个抄表计划下，那么这个抄表计划的状态不应该是“执行中” <br/>
     * 2.返回信息； <br/>
     * @param header <br/>
     * _token : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 令牌;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-user-code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户代码;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-user-name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 用户名;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _hzhr-company-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 公司编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _hzhr-dept-id : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 部门编号;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : true;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/><br/>
     * @param query <br/>
     * _page : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 页数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _count : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 每页数量;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : 1;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : int;<br/>
     * _show : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 展示形式 one:展示一个,list:展示列表,null(空值):不展示;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _show_total : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 是否展示总数;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 默认值 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : boolean;<br/>
     * _equal_meterPlanId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件：where MeterGasUserModel.meterPlanId = #{_equal_meterPlanId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _notequal_meterPlanId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件：where MeterGasUserModel.meterPlanId != #{_equal_meterPlanId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _like_code : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件：where MeterGasUserModel.code like #{_like_code} + %;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _like_name : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件：where MeterGasUserModel.name like  #{_like_name} + %;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _like_address : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件：where MeterGasUserModel.address like #{_like_address} + %;<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/>
     * _equal_typeId : <br/>&nbsp&nbsp&nbsp&nbsp 说明 : 筛选条件：where MeterGasUserModel.typeId != #{_equal_typeId};<br/>&nbsp&nbsp&nbsp&nbsp 是否必填 : false;<br/>&nbsp&nbsp&nbsp&nbsp 数据类型 : string;<br/><br/>
     * @throws ServiceException <br/>
     * @see MeterGasUserGetModel result <br/>
     */ 
    //endregion
    //region 说明 : get
    //简介：查询登录用户管辖范围内的抄表用户
    //步骤：
    //1.查询登录用户管辖范围内的并且状态为“在用”燃气用户
    //注意以下查询规则：
    //(1) 在query参数notequal_meterPlanId不为空，如果查询出的燃气用户在某个抄表计划下，那么这个抄表计划的状态不应该是“执行中”
    //2.返回信息；
    //endregion
    //region 参数 : get
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "令牌",required = true, dataType = "string", paramType = "header"),

            @ApiImplicitParam(name = "page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "equal_meterPlanId", value = "筛选条件：where MeterGasUserModel.meterPlanId = #{equal_meterPlanId}", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "notequal_meterPlanId", value = "筛选条件：where MeterGasUserModel.meterPlanId != #{equal_meterPlanId}", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "like_code", value = "筛选条件：where MeterGasUserModel.code like #{like_code} + %", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "like_name", value = "筛选条件：where MeterGasUserModel.name like  #{like_name} + %", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "like_address", value = "筛选条件：where MeterGasUserModel.address like #{like_address} + %", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "equal_typeId", value = "筛选条件：where MeterGasUserModel.typeId != #{equal_typeId}", required = false, dataType = "string", paramType = "query"),

    })
    @ApiImplicitInternalParams({
            @ApiImplicitParam(name = "_token", value = "令牌",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-id", value = "用户编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-code", value = "用户代码",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-user-name", value = "用户名",required = true, dataType = "string", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-company-id", value = "公司编号",required = true, dataType = "int", paramType = "header"),
            @ApiImplicitParam(name = "_hzhr-dept-id", value = "部门编号",required = true, dataType = "int", paramType = "header"),

            @ApiImplicitParam(name = "_page", value = "页数", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_count", value = "每页数量", defaultValue = "1",required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "_show", value = "展示形式 one:展示一个,list:展示列表,null(空值):不展示", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_show_total", value = "是否展示总数", defaultValue = "false", required = false, dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "_equal_meterPlanId", value = "筛选条件：where MeterGasUserModel.meterPlanId = #{_equal_meterPlanId}", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_notequal_meterPlanId", value = "筛选条件：where MeterGasUserModel.meterPlanId != #{_equal_meterPlanId}", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_like_code", value = "筛选条件：where MeterGasUserModel.code like #{_like_code} + %", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_like_name", value = "筛选条件：where MeterGasUserModel.name like  #{_like_name} + %", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_like_address", value = "筛选条件：where MeterGasUserModel.address like #{_like_address} + %", required = false, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "_equal_typeId", value = "筛选条件：where MeterGasUserModel.typeId != #{_equal_typeId}", required = false, dataType = "string", paramType = "query"),
    })
    //endregion
    //region 请求 : get
    @RequestMapping(value = "/meter-gas-users", method = RequestMethod.GET)
    //endregion
    public MeterGasUserGetModel get(Map<String, String> header, Map<String, Object> query) throws ServiceException {
        /* 查询登录用户管辖范围内的并且状态为“在用”燃气用户 */
        UserCardModel userCard = getUserCard(header);
        String orgId = userCard.getUserCode().replaceFirst("010009", "00").replaceFirst("01", "00");

        String meterPlanId = HzhrTypeUtil.object2string(query.get("_equal_meterPlanId")); //计划编号
        String notequal_meterPlanId = HzhrTypeUtil.object2string(query.get("_notequal_meterPlanId"));  //!= 计划编号
        String codes = HzhrTypeUtil.object2string(query.get("_like_code")); //用户号

        String code = null;
        if (codes != null){
             code = codes+"%";
        }
        String names = HzhrTypeUtil.object2string(query.get("_like_name"));  //用户名称
        String name = null;
        if (names != null){
             name = names+"%";
        }
        String address = HzhrTypeUtil.object2string(query.get("_like_address"));
        String address_1 = null;
        if (address != null){
             address_1 = address+"%";
        }
        String concentratorNo = HzhrTypeUtil.object2string(query.get("_like_concentratorNo"));  //集中器编号
        String concentratorNo_1 = null;
        if (concentratorNo != null){
            concentratorNo_1 = concentratorNo+"%";
        }

        String typeId = HzhrTypeUtil.object2string(query.get("_equal_typeId")); //用户地址

        String _show = HzhrTypeUtil.object2string(query.get("_show"));
        Boolean _show_total = HzhrTypeUtil.object2boolean(query.get("_show_total"));
        Integer _page = HzhrTypeUtil.object2integer(query.get("_page"));
        Integer _count = HzhrTypeUtil.object2integer(query.get("_count"));
        Integer start = null;

        if (_page!= null){
            start = (_page * _count)-_count +1;
        }
        _count = _page * _count;

        Integer total = null;
        if (_show_total != null && _show_total) {
            total = yongqidizhiEntityCustomMapper.selctYongqidizhiEntityExtCount(meterPlanId,notequal_meterPlanId,code,name,address_1,typeId,concentratorNo_1, "1002", orgId + "%");
        }

        //根据条件查询抄表用户
        List<YongqidizhiEntityExt> yongqidizhiEntityExts = yongqidizhiEntityCustomMapper.selctYongqidizhiEntityExt(start,_count,meterPlanId,notequal_meterPlanId,code,name,address_1,typeId,concentratorNo_1, "1002", orgId + "%");

        /* 返回信息 */
        MeterGasUserGetModel result = new MeterGasUserGetModel();
        if (yongqidizhiEntityExts.size() >0) {
            if ("one".equals(_show)) {
                YongqidizhiEntityExt yongqidizhiEntityExt_1 = yongqidizhiEntityExts.get(0);
                MeterGasUserModel meterGasUserModel = new MeterGasUserModel();
                meterGasUserModel.setCode(yongqidizhiEntityExt_1.getYqdzYqzh());
                meterGasUserModel.setName(HzhrTypeUtil.object2string(yongqidizhiEntityExt_1.getYqdzHzMc()));
                meterGasUserModel.setAddress(HzhrTypeUtil.object2string(yongqidizhiEntityExt_1.getYqdzSm()));
                meterGasUserModel.setTypeId(yongqidizhiEntityExt_1.getYqdzKhLx());
                meterGasUserModel.setTypeDes(yongqidizhiEntityExt_1.getDES());  //KHLXdes
                meterGasUserModel.setMeterPlanId(yongqidizhiEntityExt_1.getYqdzCbcId());
                result.setModel(meterGasUserModel);
            }
        }
        if (yongqidizhiEntityExts.size() ==0) {
            if ("one".equals(_show)) {
                YongqidizhiEntityExt yongqidizhiEntityExt_1 = new YongqidizhiEntityExt();
                MeterGasUserModel meterGasUserModel = new MeterGasUserModel();
                meterGasUserModel.setCode(yongqidizhiEntityExt_1.getYqdzYqzh());
                meterGasUserModel.setName(HzhrTypeUtil.object2string(yongqidizhiEntityExt_1.getYqdzHzMc()));
                meterGasUserModel.setAddress(HzhrTypeUtil.object2string(yongqidizhiEntityExt_1.getYqdzSm()));
                meterGasUserModel.setTypeId(yongqidizhiEntityExt_1.getYqdzKhLx());
                meterGasUserModel.setTypeDes(yongqidizhiEntityExt_1.getDES());  //KHLXdes
                meterGasUserModel.setMeterPlanId(yongqidizhiEntityExt_1.getYqdzCbcId());
                result.setModel(meterGasUserModel);
            }
        }

        if ("list".equals(_show)) {
            List<MeterGasUserModel> meterGasUserModels_2 = new ArrayList<>();
            if (yongqidizhiEntityExts.size()>0) {
                for (YongqidizhiEntityExt yongqidizhiEntityExt : yongqidizhiEntityExts) {
                    MeterGasUserModel meterGasUserModel = new MeterGasUserModel();
                    meterGasUserModel.setCode(yongqidizhiEntityExt.getYqdzYqzh());
                    meterGasUserModel.setName(HzhrTypeUtil.object2string(yongqidizhiEntityExt.getYqdzHzMc()));
                    meterGasUserModel.setAddress(HzhrTypeUtil.object2string(yongqidizhiEntityExt.getYqdzSm()));
                    meterGasUserModel.setTypeId(yongqidizhiEntityExt.getYqdzKhLx());
                    meterGasUserModel.setTypeDes(yongqidizhiEntityExt.getDES());  //KHLXdes
                    meterGasUserModel.setMeterPlanId(yongqidizhiEntityExt.getYqdzCbcId());
                    meterGasUserModels_2.add(meterGasUserModel);
                }
            }
            result.setModelList(meterGasUserModels_2);
        }
        result.setTotal(total);
        return result;
    }













}
